package com.iweb.controller;

import com.iweb.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 南京艾瑞
 * 作者: jack
 * 时间: 2020-06-28 11:35
 * 描述: UserController
 */
@Controller
@RequestMapping("/user") // 请求url的命名空间
public class UserController {

    // /user/findUser?name=jack&age=20
    @RequestMapping("/findUser")
    // 当请求url的key和形参的变量名一致的时候可以省略 @RequestParam
    public ModelAndView findUser(String name, Integer age) {
        System.out.println(String.format("name:%s,age:%s", name, age));
        return new ModelAndView("success");
    }

    // 请求参数中的key要和User类中的set方法名对应
    @RequestMapping(value = "/addUser", method = RequestMethod.POST) // 对应POST请求，默认是GET
    public ModelAndView addUser(User user) {
        System.out.println(user);
        return new ModelAndView("success");
    }

    @RequestMapping(value = "/collection", method = RequestMethod.POST)
    public ModelAndView collection(User user) {
        System.out.println(user.getList());
        System.out.println(user.getMap());
        return new ModelAndView("success");
    }

    /**
     * servlet 内置对象
     *
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping("/testServletAPI")
    public ModelAndView testServletAPI(HttpServletRequest request,
                                       HttpServletResponse response,
                                       HttpSession session) {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
        return new ModelAndView("success");
    }
}
